import 'package:app_admin/config.dart';
import 'package:app_admin/models/login_certificate.dart';
import 'package:app_admin/models/upgrade_info.dart';
import 'package:app_admin/models/user_full_info.dart';
import 'package:app_admin/res/strings.dart';
import 'package:app_admin/utils/HttpUtil.dart';
import 'package:app_admin/utils/data_sp.dart';
import 'package:app_admin/utils/logger.dart';
import 'package:app_admin/utils/utils.dart';
import 'package:app_admin/widgets/views.dart';
import 'package:collection/collection.dart';
import 'package:dio/dio.dart';

class Urls {
  static final getUsersFullInfo = "${Config.appAuthUrl}/user/find/full";
  static final queryUserInfo = "${Config.appAuthUrl}/user/info";
  static final getVerificationCode = "${Config.appAuthUrl}/account/code/send";
  static final checkVerificationCode =
      "${Config.appAuthUrl}/account/code/verify";

  static final login = "${Config.appAuthUrl}/account/login";
  static final upgrade = "${Config.appAuthUrl}/app/check";
}

class Apis {
  static Options get imTokenOptions =>
      Options(headers: {'token': DataSp.imToken});

  static Options get chatTokenOptions =>
      Options(headers: {'token': DataSp.chatToken});

  static Future<LoginCertificate> login({
    String? areaCode,
    String? phoneNumber,
    String? email,
    String? password,
    String? verificationCode,
  }) async {
    try {
      var data = await HttpUtil.post(Urls.login, data: {
        "areaCode": areaCode,
        'phoneNumber': phoneNumber,
        'email': email,
        'password': null != password ? IMUtils.generateMD5(password) : null,
        'platform': IMUtils.getPlatform(),
        'verifyCode': verificationCode,
      });
      return LoginCertificate.fromJson(data!);
    } catch (e, s) {
      Logger.print('e:$e s:$s');
      return Future.error(e);
    }
  }

  //===获取玩家信息
  static Future<UserFullInfo?> queryMyFullInfo() async {
    final list = await Apis.getUserFullInfo(
      userIDList: ["2134"],
    );
    return list?.firstOrNull;
  }

  static Future<List<UserFullInfo>?> getUserFullInfo({
    int pageNumber = 0,
    int showNumber = 10,
    required List<String> userIDList,
  }) async {
    final data = await HttpUtil.post(
      Urls.getUsersFullInfo,
      data: {
        'pagination': {'pageNumber': pageNumber, 'showNumber': showNumber},
        'userIDs': userIDList,
        'platform': IMUtils.getPlatform(),
      },
      options: chatTokenOptions,
    );
    if (data['users'] is List) {
      return (data['users'] as List)
          .map((e) => UserFullInfo.fromJson(e))
          .toList();
    }
    return null;
  }

  ///===版本更新
  static Future<UpgradeInfoV2> checkUpgradeV2() {
    return dio.post<Map<String, dynamic>>(
      '',
      options: Options(
        contentType: 'application/x-www-form-urlencoded',
      ),
      data: {
        '_api_key': '',
        'appKey': '',
      },
    ).then((resp) {
      Map<String, dynamic> map = resp.data!;
      if (map['code'] == 0) {
        return UpgradeInfoV2.fromJson(map['data']);
      }
      return Future.error(map);
    });
  }
}
